package cn.itsource.user.service.impl;

import cn.itsource.basic.constant.BaseConstants;
import cn.itsource.basic.dto.PhoneRegisterDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.LogininfoMapper;
import cn.itsource.user.mapper.UserMapper;
import cn.itsource.user.service.IUserService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zj
 * @since 2022-12-03
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 手机号注册实现
     * @param phoneRegisterDto
     */
    @Override
    public void phoneRegister(PhoneRegisterDto phoneRegisterDto) {
//        1.参数非空校验 //TODO 自研
//        2.校验密码和确认密码是否一致
        if (!phoneRegisterDto.getPassword().equals(phoneRegisterDto.getConfirmPassword())){
            throw new BusinessException("两次密码不等，你是属鱼的吗？");
        }
//        3.校验手机验证码是否正确
        Object smsCodeAndTime = redisTemplate.opsForValue().get(BaseConstants.VerifyCodeConstants.USER_PHONE_REGISTER_PREFIX + phoneRegisterDto.getPhone());//9527:4894978
        if (smsCodeAndTime == null){
            throw new BusinessException("验证码已过期");
        }
        String[] smsCodeAndTimeArr = smsCodeAndTime.toString().split(":");
        if (!phoneRegisterDto.getSmsCode().equalsIgnoreCase(smsCodeAndTimeArr[0])){
            throw new BusinessException("手机验证码错误");
        }
//        4.保存loginInfo 和 user表
        //初始化user ctrl + alt + m
        User user = phoneRegisterDto2User(phoneRegisterDto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        //把logininfo的id设置给user
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }



    private User phoneRegisterDto2User(PhoneRegisterDto phoneRegisterDto) {
        User user = new User();
        user.setPhone(phoneRegisterDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        user.setPassword(MD5Utils.encrypByMd5(phoneRegisterDto.getPassword() + salt));//用户的密码需要加密加盐
        user.setCreatetime(new Date());
        user.setState(1);
        user.setUsername(phoneRegisterDto.getPhone());
        return user;
    }

    private Logininfo user2Logininfo(User user){
        Logininfo logininfo = new Logininfo();
        //使用对象拷贝的方法
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setDisable(false);//默认账号启用
        logininfo.setType(true); //true 1 :用户    false 0:员工
        return logininfo;
    }
}
